package spring.demo.security.userService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import spring.demo.security.dao.UserRepository;
import spring.demo.security.entity.SecurityUser;
import spring.demo.security.entity.SysUser;

/**
 * Created by td on 2017/10/11.
 */
@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;
    
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        SysUser sysUser = userRepository.findByName(s);
        if (sysUser == null) {
            throw new UsernameNotFoundException("UserName "+s +" not found");
        }
        SecurityUser securityUser = new SecurityUser(sysUser);
        return securityUser;
    }

}
